<template>
  <div>
    <!-- title -->
    <PageTitle>
      {{ $t('menu.M_10408004') }}
    </PageTitle>
    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      @current-change="currentChangeHandle"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
    >
      <el-form class="p-4" inline>
        <el-form-item>
          <el-input
            maxlength="64"
            v-model="condition"
            @keyup.enter.native="fetchData((pageIndex = 1))"
            @blur="() => (condition = condition.trim())"
            :placeholder="$t('role.role_label')"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-8"
            @click="fetchData((pageIndex = 1))"
          >
            <svg-icon icon-class="search"/>
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="isAuth('10408004.MANAGE')"
            type="primary"
            class="w-8"
            @click="handleAdd"
          >
            <svg-icon icon-class="plus"/>
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        :show-overflow-tooltip="true"
        v-loading="listLoading"
        :data="tableData"
        stripe
      >
        <el-table-column
          :label="$t('role.role_label')"
          prop="roleName" 
        />
        <el-table-column
          :label="$t('role.role_user_amount')"
          prop="userCounts"
        >
          <!--          <template v-slot="scope">1</template>-->
        </el-table-column>
        <el-table-column
          :label="$t('common.createTime')"
          prop="creTime"
        >
          <template v-slot="{ row }">
            <div :title="timeFilter(row.creTime)">
              {{ dateFilter(row.creTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="isAuth('10408004.MANAGE')"
          width="100px"
        >
          <template v-slot="scope">
            <div :title="$t('common.modify')" class="mr-1 inline-block"><el-button class="w-8" @click="handleEdit(scope.row)" text ><svg-icon icon-class="Edit"/></el-button></div>
            <div :title="$t('common.delete')" class="inline-block"><el-button class="w-8" style="color:var(--el-color-danger)" text @click="handleDelete(scope.row)"><svg-icon icon-class="Delete"/></el-button></div>
            <!-- <el-dropdown
              class="align-middle"
              trigger="click"
            >
              <el-button text>
                <svg-icon icon-class="MoreFilled" />
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleEdit(scope.row)">
                    <svg-icon icon-class="Edit"/>
                    {{ $t('common.modify') }}
                  </el-dropdown-item>
                  <el-dropdown-item @click="handleDelete(scope.row)">
                    <svg-icon icon-class="Delete"/>
                    {{ $t('common.delete') }}
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown> -->
          </template>
        </el-table-column>
            <!-- <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.modify')"
              placement="top"
            >
              <el-button
                type="text"
                style="color: #8c939d"
                @click="handleEdit(scope.row)"
                icon="el-icon-edit"
              />
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.delete')"
              placement="top"
            >
              <el-button
                type="text"
                style="color: #dc143c"
                @click="handleDelete(scope.row)"
                icon="el-icon-delete"
              />
            </el-tooltip> -->
      </el-table>
    </el-card>
    <role-add
      :type="operationType"
      :role-id="editRoleId"
      @submit="fetchData"
      v-model="addRoleVisible"
      :title="
        editRoleId ? $t('role.edit_role_title') : $t('role.add_role_title')
      "
    ></role-add>
  </div>
</template>

<script>
import { getRoleList, deleteRole } from '@/api/role'
import { timeFilter, dateFilter } from '@/filters'
import RoleAdd from './role-add'
import { parseTime } from '@/utils'
import { isAuth } from '@/utils'
import PageTitle from '@/components/PageTitle/index.vue'
export default {
  components: {
    RoleAdd,
    PageTitle
  },
  data() {
    return {
      tableData: [],
      listLoading: false,
      selectedData: [],
      paginationShow: true,
      condition: '',
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addRoleVisible: false,
      operationType: '',
      editRoleId: ''
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    isAuth,
    statusFilter(status) {
      const statusMap = {
        0: 'Invalid',
        1: 'Valid'
      }
      return statusMap[status]
    },
    timeFilter,
    dateFilter,
    fetchData() {
      this.listLoading = true
      getRoleList({
        pageNo: this.pageIndex,
        pageSize: this.pageSize,
        condition: this.condition,
        platCode: 'TOMS'
      })
        .then((res) => {
          this.listLoading = false
          this.tableData = res.data
          this.totalPage = res.rows
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })
    },
    handleClick(val) {
      let command = val.command
      let row = val.row
      if (command === 'edit') {
        this.handleEdit(row)
      } else if (command === 'delete') {
        this.handleDelete(row)
      }
    },
    beforeHandleCommand(command, row) {
      return {
        row: row,
        command: command
      }
    },
    handleEdit(row) {
      this.editRoleId = row.roleId
      this.operationType = 'update'
      this.addRoleVisible = true
    },
    handleAdd() {
      this.editRoleId = ''
      this.operationType = 'add'
      this.addRoleVisible = true
    },
    handleDelete(row) {
      this.$confirm(
        this.$t('role.role_delete_confirm') + `[${row.roleName}]?`,
        this.$t('common.tips'),
        {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }
      ).then(() => {
        deleteRole(row.roleId)
          .then(() => {
            this.$message({
              message: this.$t('role.delete_success'),
              type: 'success'
            })
            this.fetchData()
          })
          .catch(this.$confirmError)
      })
    },
    formatDate(row, column, cellValue, index) {
      return parseTime(cellValue, '{y}-{m}-{d}')
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.fetchData()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss">
.terminal-list {
  .el-table .cell {
    /* height: 30px !important; */
  }
}
</style>

<style lang="scss" scoped>
.query-button {
  margin-left: 30px;
}
.svg-container {
  cursor: pointer;
  margin-top: 6px;
  margin-left: 20px;
  font-size: 24px;
  display: inline-block;
}

.pagination-wrapper {
  text-align: right;
}
</style>
